package com.whut.rssReader.activity;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableAdapter extends BaseExpandableListAdapter {
    	
    	 private Context context; 
    	 private List<Map<String, String>> groups; 
    	 private List<List<Map<String, String>>> childs; 
    	 
    	 public ExpandableAdapter(Context context,List<Map<String, String>> groups,List<List<Map<String, String>>> childs){
    		 this.context = context;
    		 this.groups = groups;
    		 this.childs = childs;
    	 }

		@Override
		public int getGroupCount() {
			return groups.size();
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return childs.get(groupPosition).size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return groups.get(groupPosition);
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return childs.get(groupPosition).get(childPosition);
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			LayoutInflater layoutInflater = LayoutInflater.from(context);
			View view = layoutInflater.inflate(R.layout.expandable_groups, null);
			TextView groupTo = (TextView) view.findViewById(R.id.groupTo);
			String sort_name = groups.get(groupPosition).get("groups") + "(" + childs.get(groupPosition).size() + ")";
			groupTo.setText(sort_name);
			return view;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			LayoutInflater layoutInflater = LayoutInflater.from(context);
			View view = layoutInflater.inflate(R.layout.expandable_childs, null);
			TextView childTo = (TextView) view.findViewById(R.id.childTo);
			TextView resource_link = (TextView) view.findViewById(R.id.resource_link);
			TextView resource_id = (TextView) view.findViewById(R.id.resource_id);
			childTo.setText(childs.get(groupPosition).get(childPosition).get("childs"));
			resource_link.setText(childs.get(groupPosition).get(childPosition).get("resource_link"));
			resource_id.setText(childs.get(groupPosition).get(childPosition).get("resource_id"));
			return view;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		
    }